package com.qidianit.mes.pro.controller;

import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.Process;
import com.qidianit.mes.pro.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description:
 * @Author: shanzhe
 * @Date: 2025/9/17 - 14:14
 */
@RestController
@RequestMapping("/process")
@CrossOrigin
public class ProcessController {
    @Autowired
    private ProcessService processService;

    @GetMapping("/page")
    public Result<PageInfo<Process>> queryProcessListByPageAndCondition(@RequestParam(value = "processCode",required = false) String processCode,
                                                                        @RequestParam(value = "processName",required = false) String processName,
                                                                        @RequestParam(value = "isActive",required = false) Integer isActive,
                                                                        @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                                        @RequestParam(value = "size",defaultValue = "10") Integer size)
    {
        System.out.println(processService.queryProcessListByPageAndCondition(processCode,processName,isActive,page,size));
       return Result.success(processService.queryProcessListByPageAndCondition(processCode,processName,isActive,page,size));
    }

    @PostMapping("/add")
   public Result addProcess(@RequestBody Process process)
    {
        process.setCreateTime(LocalDateTime.now());
        process.setUpdateTime(LocalDateTime.now());
        process.setCreatorId(1001);
        int result = processService.addProcess(process);
        return result ==1?Result.success(process):result ==-1?Result.error("code重复"):Result.error("操作失败");
    }

    @GetMapping("/code/{code}")
    public Result queryProcessByProcessCode(@PathVariable("code") String code) {
        Process process = processService.queryProcessByProcessCode(code);
       if (process == null)
       {
           return Result.error("操作失败");
       }
       else
       {
           return Result.success(process);
       }
    }

    @PutMapping("/update")
    public Result updateProcess(@RequestBody Process process) {
        process.setUpdateTime(LocalDateTime.now());
        process.setModifierId(1002);
        int result = processService.updateProcessById(process);
        return result ==1?Result.success():result ==-1?Result.error("code重复"):Result.error("操作失败");
    }

/** Todo:添加修改人*/

    @DeleteMapping("/delete/{id}")
    public Result deleteProcessById(@PathVariable("id") Integer id) {
        int result = processService.deleteProcessById(id);
        return result == 1?Result.success():Result.error("操作失败");
    }
    /** Todo:添加修改人*/

    @DeleteMapping("/batchDelete")
    public Result batchDeleteProcess(@RequestParam List<Integer> id) {
        int result = processService.deleteProcessByListIds(id);

        return result == 1?Result.success():result == -1?Result.error("删除了部分"):Result.error("操作失败");
    }
 }


